<?php

namespace app\dao\exam;

use app\dao\BaseDao;
use app\model\exam\ExamRecord;
use app\model\exam\ExamSend;

class ExamSendDao extends BaseDao
{

    public function index(array $where)
    {
        return $this->search()->with(['OfficeClass' => function ($query) {
            $query->withField(['id', 'title']);
        }, 'paper' => function ($query) {
            $query->withField(['id', 'title']);
        }])->withSearch(['title', 'pattern'], ['title' =>
            $where['title'], 'pattern' => $where['pattern']])->
        where('website_id', $where['website_id'])->order('id', 'desc')->paginate($where['pageSize']);
    }

    public function create(array $where)
    {
        unset($where['id']);
        return $this->getModel()->create($where);
    }

    public function updateDao(array $where)
    {
        unset($where['website_id']);
        return $this->search()->update($where);
    }

    public function deleteDao(array $where)
    {
        return $this->getModel()->whereIn('id', $where['id'])->update(['delete_time' => date('Y-m-d H:i:s', time())]);
    }


    protected function setModel(): string
    {
        return ExamSend::class;
    }

//------------------------------------------api---------------------------------------------------------------
/*
 * 获取综合测评测试目录
 */
    public function getExam(array $where){
        return $this->search()
            ->where('website_id', $where['website_id'])
            ->where('class_id',$where['class_id'])
            ->where('status', 1)
            ->where('pattern', 1)
            ->order('id', 'asc')
            ->withoutField(['create_time', 'update_time', 'delete_time', 'status'])
            ->select();

    }

    public function getUserExam(array $where){
        return ExamRecord::
            where('idcard',$where['idcard'])
            ->where('exam_id',$where['exam_id'])
            ->whereIn('class_id',$where['class_id'])
            ->where('exam_type',1)
            ->field(['id','exam_id','total_score',])
            ->select();
    }


}